<!--
* description: 财务管理-网点资金账户查询
* author: wangjuan
* createdDate: 2021-3-3
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-more-btn="true"
      :dynamic-is-show-select="false"
      :dynamic-is-init-table="true"
    />
  </div>
</template>
<script>

import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Vue from 'vue'
import api from '@/api/pa/financialManagement'

export default {
  name: 'BranchFundAccountInquiry',

  components: {
    OneTableTemplate
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ''], function() {
      next()
    })
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode])

    return {
      // 网格查询API配置对象
      apiConfig: api.queryDlrCapitalAccountInfo,

      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          name: 'search',
          clickEvent: () => this.queryTable(1),
          text: this.$t('sys.button.query'), // 查询
          position: 'right',
          fuzzySearch: true
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          name: 'export',
          clickEvent: () => this.exportExcel(),
          text: this.$t('sys.button.export'),
          position: 'left'
        },
        {
          compKey: 'btnKey3',
          size: 'small',
          name: 'reset',
          clickEvent: () => this.reset(),
          text: this.$t('sys.button.reset'),
          position: 'right' // 重置
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            {
              compKey: 'compKey1',
              labelName: '账户类型：',
              codeField: 'accountType',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'isCashSettle,isCashSettleCn',
              options: [
                { isCashSettle: '1', isCashSettleCn: '现金账户' },
                { isCashSettle: '2', isCashSettleCn: '信用额度' }
              ],
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey2',
              labelName: '网点类型：',
              codeField: 'dlrType',
              component: () => import('@/components/org/LookupValue'),
              oFields: 'dlrType,dlrTypeCn',
              options: [
                { dlrType: '1', dlrTypeCn: '直营店' },
                { dlrType: '2', dlrTypeCn: '授权店' }
              ],
              type: 'dropdownList',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey3',
              labelName: '网点编码：',
              codeField: 'dlrCode',
              component: () => import('@/components/pa/dlrCode'),
              type: 'inputText',
              isMul: false,
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '网点名称：',
              codeField: 'dlrShortName',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              disabled: true,
              isMul: false,
              isMust: true
            }
          ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'dlrAccountId',
              label: '账户ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'dlrId',
              label: '专营店ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'updateControlId',
              label: '并发控制ID',
              hidden: true,
              alwaysHidden: true
            },
            {
              prop: 'dlrCode',
              label: '网点编码'
            },
            {
              prop: 'dlrShortName',
              label: '网点名称'
            },
            {
              prop: 'dlrTypeCn',
              label: '网点类型'
            },
            {
              prop: 'isCashSettleCn',
              label: '现金用户'
            },
            {
              prop: 'isCreditSettleCn',
              label: '信用额度用户'
            },
            {
              prop: 'dlrBalance',
              label: '账面金额'
            },
            {
              prop: 'availableBalance',
              label: '可用金额'
            },
            {
              prop: 'frozenMoney',
              label: '冻结金额'
            },
            {
              prop: 'usedQuota',
              label: '已用额度'
            },
            {
              prop: 'creditQuota',
              label: '信用额度'
            },
            {
              prop: 'frozenQuota',
              label: '冻结额度'
            },
            {
              prop: 'availableQuota',
              label: '可用额度'
            }
          ],

      // 表单查询数据
      formField: this.$utils.getFormField(this, {
        accountType: '',
        isCashSettle: '',
        dlrShortName: '',
        dlrType: '',
        dlrCode: '',
        isEnable: '',
        dlrId: '',
        dlrAccountId: '',
        updateControlId: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },
  watch: {
    'formField.dlrCode': function (val, oldval) {
      if (!val) {
        this.formField.dlrShortName = ''
        this.formField.dlrId = ''
        this.formField.dlrType = ''
      }
    }
  },

  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    // 导出
    exportExcel() {
      const fileName = this.$utils.fileNameWithTime('网点资金账户查询.xlsx')
      this.$requestAPI({
        apiUrl: api.exportDetails.APIUrl,
        variables: { dataType: 'excel' }
      }).then(res => {
        this.$utils.downloadFile(res, fileName)
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>
